package com.android.baseconfig.data.http.retrofit2;

import com.android.baseconfig.constact.AppGlobalHost;
import com.android.baseconfig.data.http.okhttp.OkHttpManager;
import com.android.baseconfig.data.http.okhttp.interceptor.HeTNetworkLoadingInterceptor;
import com.android.baseconfig.data.http.okhttp.util.OkHttpTag;
import com.android.baseconfig.data.http.retrofit2.converter.GsonConverterFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Modifier;
import java.util.List;

import okhttp3.Call;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;

public class RetrofitManager {

    protected static Retrofit.Builder builder;

    protected static Retrofit retrofit;

    private RetrofitManager() {
    }

    private static void create() {
//        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
//        builder = new Retrofit.Builder()
//                .baseUrl(AppGlobalHost.getHost())
//                .client(OkHttpManager.getClient())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create(gson));
        builder = getRetrofitBuilder(OkHttpManager.getClient());
    }

    public static Retrofit getRetrofit(OkHttpTag okHttpTag) {
        if (builder == null) {
            create();
        }
//        return builder.build();
        retrofit = createRetrofit(builder.build(), okHttpTag);
        return retrofit;
    }

    private static Retrofit.Builder getRetrofitBuilder(OkHttpClient client) {
        return getRetrofitBuilder(client, AppGlobalHost.getHost());
    }

    public static Retrofit.Builder getRetrofitBuilder(OkHttpClient client, String host) {
//        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
        Gson gson = new GsonBuilder()
                .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
                .serializeNulls()
                .create();

//        Gson gson = GsonUtil.getInstance().getGson();
        return new Retrofit.Builder()
                .baseUrl(host)
                .client(client)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson));
    }

    public static Retrofit getRetrofitByOkHttp(OkHttpClient client) {
        return getRetrofitBuilder(client).build();
    }

    public static Retrofit createRetrofit(Retrofit retrofit, OkHttpTag okHttpTag) {
        Call.Factory factory = retrofit.callFactory();
        if (factory instanceof OkHttpClient) {
            OkHttpClient okHttpClient = (OkHttpClient) factory;
            List<Interceptor> interceptors = okHttpClient.interceptors();
            if (interceptors != null) {
                for (Interceptor interceptor : interceptors) {
                    if (interceptor instanceof HeTNetworkLoadingInterceptor) {
                        HeTNetworkLoadingInterceptor loadingInterceptor =
                                (HeTNetworkLoadingInterceptor) interceptor;
                        loadingInterceptor.setOkHttpTag(okHttpTag);
                        break;
                    }
                }
            }
        }
        return retrofit;
    }


    public static void setOkHttp(Retrofit retrofit, OkHttpTag okHttpTag) {
        Call.Factory factory = retrofit.callFactory();
        if (factory instanceof OkHttpClient) {
            OkHttpClient okHttpClient = (OkHttpClient) factory;
            List<Interceptor> interceptors = okHttpClient.interceptors();
            if (interceptors != null) {
                for (Interceptor interceptor : interceptors) {
                    if (interceptor instanceof HeTNetworkLoadingInterceptor) {
                        HeTNetworkLoadingInterceptor loadingInterceptor =
                                (HeTNetworkLoadingInterceptor) interceptor;
                        loadingInterceptor.setOkHttpTag(okHttpTag);
                        break;
                    }
                }
            }
        }
    }

    public static void release() {
        builder = null;
    }

    public static Retrofit.Builder getBuilder(){
        return builder;
    }

    public static Retrofit getRetrofit(){
        return retrofit;
    }

}
